<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: nettsvar_lastned.php 6 2005-07-11 23:14:47Z say $
 */
 
include("db.php");
include("laivid.php");

$id=$_REQUEST['id'];

$sql = "SELECT * FROM dokumenter WHERE ID=$id";
$dokumenter=mysql_query($sql);

$axx=false;

if (mysql_num_rows($dokumenter)) {
	$dokumentet=mysql_fetch_assoc($dokumenter);

	if ($dokumentet['Offentlig']) {
		$axx=true;
	} else {

		$laivid=$dokumentet['LaivID'];

		$ferdige = mysql_query("SELECT Roller_release < NOW() FROM Laiv WHERE ID=$laivid");
		$ferdig=mysql_fetch_row($ferdige);
	
		$finit = $ferdig[0];
	
		if ($finit && $dokumentet['Offentlig_etterlaiv']) {
			$axx=true;
		} else {
			$uid=$_SESSION['UserID'];
			$sql="SELECT * FROM dok_tilganger WHERE dokID=$id AND deltakerID=$uid";
			$tilgang=mysql_query($sql);

			if (mysql_errno() == 0 && mysql_num_rows($tilgang))
				$axx=true;
		}
	}

	if ($axx) {
		header ("Content-type: ".$dokumentet['datatype']);
		header ("Content-length: ".$dokumentet['str']);
		header ("Content-Disposition: inline; filename=".$dokumentet['filnavn']);
		header ("Content-Description: PHP Generated Data"); 
		echo $dokumentet['data'];
	} else
		echo "Du har ikke tilgang til dette dokumentet.";
} else
	echo "Dokumentet fins ikke.";
?>
